#!/bin/bash

# 擎动智造自动化执行平台 - 测试脚本

echo "========================================"
echo "🧪 系统环境检测"
echo "========================================"
echo ""

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

PASS_COUNT=0
FAIL_COUNT=0

# 检查Python
echo "📌 检查 Python..."
if command -v python3 &> /dev/null; then
    PYTHON_VERSION=$(python3 --version 2>&1)
    echo "✅ $PYTHON_VERSION"
    PASS_COUNT=$((PASS_COUNT + 1))
elif command -v python &> /dev/null; then
    PYTHON_VERSION=$(python --version 2>&1)
    echo "✅ $PYTHON_VERSION"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "❌ Python 未安装"
    echo "   请安装 Python 3.8+"
    FAIL_COUNT=$((FAIL_COUNT + 1))
fi
echo ""

# 检查Node.js
echo "📌 检查 Node.js..."
if command -v node &> /dev/null; then
    NODE_VERSION=$(node --version 2>&1)
    echo "✅ Node.js $NODE_VERSION"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "⚠️  Node.js 未安装"
    echo "   前端构建需要 Node.js 16+"
    echo "   如果只使用后端API，可以跳过"
fi
echo ""

# 检查npm
echo "📌 检查 npm..."
if command -v npm &> /dev/null; then
    NPM_VERSION=$(npm --version 2>&1)
    echo "✅ npm $NPM_VERSION"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "⚠️  npm 未安装"
fi
echo ""

# 检查pip
echo "📌 检查 pip..."
if command -v pip3 &> /dev/null; then
    PIP_VERSION=$(pip3 --version 2>&1)
    echo "✅ $PIP_VERSION"
    PASS_COUNT=$((PASS_COUNT + 1))
elif command -v pip &> /dev/null; then
    PIP_VERSION=$(pip --version 2>&1)
    echo "✅ $PIP_VERSION"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "❌ pip 未安装"
    FAIL_COUNT=$((FAIL_COUNT + 1))
fi
echo ""

# 检查端口
echo "📌 检查端口 10001..."
if lsof -Pi :10001 -sTCP:LISTEN -t >/dev/null 2>&1; then
    echo "⚠️  端口 10001 已被占用"
    echo "   运行 ./stop.sh 停止服务"
else
    echo "✅ 端口 10001 可用"
    PASS_COUNT=$((PASS_COUNT + 1))
fi
echo ""

# 检查项目文件
echo "📌 检查项目文件..."
if [ -f "core/web/main.py" ]; then
    echo "✅ 后端文件存在"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "❌ 后端文件缺失"
    FAIL_COUNT=$((FAIL_COUNT + 1))
fi

if [ -f "core/web/requirements.txt" ]; then
    echo "✅ Python依赖文件存在"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "❌ requirements.txt 缺失"
    FAIL_COUNT=$((FAIL_COUNT + 1))
fi

if [ -d "ui/src" ]; then
    echo "✅ 前端源码存在"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "❌ 前端源码缺失"
    FAIL_COUNT=$((FAIL_COUNT + 1))
fi

if [ -f "ui/package.json" ]; then
    echo "✅ 前端配置文件存在"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "❌ package.json 缺失"
    FAIL_COUNT=$((FAIL_COUNT + 1))
fi
echo ""

# 检查前端是否已构建
echo "📌 检查前端构建..."
if [ -d "ui/dist" ] && [ -f "ui/dist/index.html" ]; then
    echo "✅ 前端已构建"
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "⚠️  前端未构建"
    echo "   运行 ./run.sh 会自动构建"
fi
echo ""

# 检查Python依赖
echo "📌 检查 Python 依赖..."
cd core/web
PYTHON_CMD="python3"
if ! command -v python3 &> /dev/null; then
    PYTHON_CMD="python"
fi

MISSING_DEPS=0
for dep in fastapi uvicorn pydantic; do
    if $PYTHON_CMD -c "import $dep" 2>/dev/null; then
        echo "✅ $dep 已安装"
    else
        echo "❌ $dep 未安装"
        MISSING_DEPS=$((MISSING_DEPS + 1))
    fi
done

if [ $MISSING_DEPS -eq 0 ]; then
    PASS_COUNT=$((PASS_COUNT + 1))
else
    echo "   运行安装: pip install -r requirements.txt"
    FAIL_COUNT=$((FAIL_COUNT + 1))
fi

cd "$SCRIPT_DIR"
echo ""

# 检查Node依赖
if command -v node &> /dev/null; then
    echo "📌 检查 Node 依赖..."
    if [ -d "ui/node_modules" ]; then
        echo "✅ Node依赖已安装"
        PASS_COUNT=$((PASS_COUNT + 1))
    else
        echo "⚠️  Node依赖未安装"
        echo "   运行安装: cd ui && npm install"
    fi
    echo ""
fi

# 总结
echo "========================================"
echo "📊 检测结果"
echo "========================================"
echo ""
echo "✅ 通过: $PASS_COUNT 项"
if [ $FAIL_COUNT -gt 0 ]; then
    echo "❌ 失败: $FAIL_COUNT 项"
fi
echo ""

if [ $FAIL_COUNT -eq 0 ]; then
    echo "🎉 系统环境检测通过！"
    echo ""
    echo "💡 下一步:"
    echo "   1. 运行 ./run.sh 启动服务"
    echo "   2. 访问 http://localhost:10001"
    echo "   3. 使用账号 admin/admin123 登录"
else
    echo "⚠️  请解决上述问题后再启动服务"
    echo ""
    echo "💡 常见解决方案:"
    echo "   - 安装Python: https://www.python.org/downloads/"
    echo "   - 安装Node.js: https://nodejs.org/"
    echo "   - 安装依赖: pip install -r core/web/requirements.txt"
fi
echo ""
echo "========================================"

